﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GoldenAgeHelper.DAL;
using GoldenAgeHelper.common;

namespace GoldenAgeHelper
{
    public partial class frmMap : Form
    {
        public frmMap()
        {
            InitializeComponent();
        }

        public void AddCity(int cid)
        {
            if (cids == null)
            {
                cids = new List<int>();
            }
            cids.Add(cid);
        }

        public void AddCityNone(int cid)
        {
            cids2.Add(cid);
        }

        PointF[] point1;
        PointF[] point2;
        List<int> cids = new List<int>();
        List<int> cids2 = new List<int>();

        Image img = Image.FromFile("map/i_hw.gif");
        Image img2 = Image.FromFile("map/i_hw2.gif");
        Image img3 = Image.FromFile("map/i_hw3.gif");

        private void frmMap_Load(object sender, EventArgs e)
        {
            point1 = new PointF[cids.Count];
            point2 = new PointF[cids2.Count];
            for (int i = 0; i < cids.Count; i++)
            {
                int cid = cids[i];
                if (cid == 0)
                {
                    continue;
                }
                List<object> list = City.GetCityPoint(cid);
                point1[i] = new PointF(Convert.ToSingle(list[0]), Convert.ToSingle(list[1]));
            }
            for (int i = 0; i < cids2.Count; i++)
            {
                int cid = cids2[i];
                if (cid == 0)
                {
                    continue;
                }
                List<object> list = City.GetCityPoint(cid);
                point2[i] = new PointF(Convert.ToSingle(list[0]), Convert.ToSingle(list[1]));
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (point1 == null || point2 == null)
            {
                return;
            }

            for (int i = 0; i < point1.Length; i++)//绘制存在货物的城市
            {
                float fx = point1[i].X * 5 + 280;
                float fy = point1[i].Y * 5 - 150;
                e.Graphics.DrawImage(img, new PointF(fx, -fy));
                //e.Graphics.DrawString("[" + point1[i].X + "," + point1[i].Y + "]", new Font("宋体", 8), new SolidBrush(Color.Red), new PointF(fx, -fy));
            }
            for (int i = 0; i < point2.Length; i++)//绘制没有货物的城市
            {

                float fx = point2[i].X * 5 + 280;
                float fy = point2[i].Y * 5 - 150;
                if (cids2.Contains(cids[i]))
                {
                    e.Graphics.DrawImage(img3, new PointF(fx, -fy));
                }
                else
                {
                    e.Graphics.DrawImage(img2, new PointF(fx, -fy));
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_MouseHover(object sender, EventArgs e)
        {

        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {

        }

        int currentid = 0;
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            int x = (e.X - 280) / 5;
            int y = -(e.Y - 150) / 5;
            int error = 2;

            this.lblPos.Text = x + "," + y;

            DataView view = City.dtcity.DefaultView;
            view.RowFilter = "x>" + (x - error) + " and x<" + (x + error) + " and y>" + (y - error) + " and y<" + (y + error);
            DataTable dt = view.ToTable();
            if (dt.Rows.Count > 0)
            {

                panel2.Visible = true;
                this.label2.Text = dt.Rows[0]["cname"].ToString();
                this.pictureBox1.Cursor = Cursors.Hand;

                if (currentid == Convert.ToInt32(dt.Rows[0]["cid"]))
                {
                    return;
                }
                currentid = Convert.ToInt32(dt.Rows[0]["cid"]);


                for (int i = 0; i < panel2.Controls.Count; i++)
                {
                    panel2.Controls.RemoveAt(i);
                }
                //panel2.ResumeLayout();
                //panel2.Update();
                //panel2.Height = 1;



                DataTable dth = CityHuowu.GetCityHuowu(Convert.ToInt32(dt.Rows[0]["cid"]));
                if (dth.Rows.Count > 0)
                {
                    //Panel panel3 = new Panel();
                    if (panel2.Controls.Count > 0)
                    {
                        panel2.Location = new Point(x * 5 + 280 + 15, -y * 5 + 150 + 15);
                        panel2.Width = 250;
                        panel2.Height = 30 + 16 * dth.Rows.Count / 2;

                        Label l = new Label();
                        l.Text = dt.Rows[0]["cname"].ToString();
                        panel2.Controls.Add(l);
                        int top = 20;
                        for (int i = 0; i < dth.Rows.Count; i++)
                        {
                            Label huowu = new Label();
                            huowu.Left = i % 2 == 0 ? 5 : 150;
                            huowu.Top = top + (16 * (i / 2));
                            huowu.Height = 12;
                            huowu.Width = 95;
                            huowu.Text = dth.Rows[i]["huowuname"].ToString() + ":" + Util.GetFormatMoney(Convert.ToInt32(dth.Rows[i]["price"]));
                            //huowu.BackColor = Color.Red;
                            panel2.Controls.Add(huowu);
                        }
                        panel2.Focus();
                    }
                    panel2.Update();
                    //panel2.ResumeLayout();
                    this.Update();

                    panel2.CreateControl();

                    currentid = Convert.ToInt32(dt.Rows[0]["cid"]);
                }
                else
                {
                    panel2.Visible = false;
                }
                //Panel panel = new Panel();

            }
            else
            {
                panel2.Visible = false;
                this.pictureBox1.Cursor = Cursors.Default;
                this.label2.Text = "当前坐标：";
            }
        }
    }
}